package com.cg.controller;

import com.cg.dto.*;
import com.cg.entity.User;
import com.cg.service.UserService;
import com.cg.vo.LoginUserVo;
import com.cg.vo.UserDepRoleVo;
import com.cg.vo.UserDetailVo;
import com.cg.vo.UserShowInfoVo;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (User)表控制层
 *
 * @author chengong
 * @since 2023-12-01 11:49:44
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    /**
     * 用UUID生成codeNum
     */
    @PostMapping("/createCodeNum")
    public Result<String> createCodeNum(){
        String codeNum = userService.createCodeNum();
        return ResultUtils.returnDataSuccess(codeNum);
    }


    /**
     * 添加新用户
     * @param user:前端传过来要添加的用户信息
     */
    @PostMapping("/createUser")
    public Result createUser(@RequestBody User user){
        Integer count = userService.createUser(user);
        if(count.equals(0)){
            return ResultUtils.returnResult(ResultEnum.FAIL_OPERATE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 用户登录
     * @param user：前端传递过来的用户登录名和密码
     * @return：返回的数据
     */
    @PostMapping("/login")
    public Result<LoginUserVo> login(@RequestBody User user){
        LoginUserVo loginUserVo = userService.login(user);
        if(loginUserVo == null){
            return ResultUtils.returnResult(ResultEnum.FAIL_LOGIN);
        }
        return ResultUtils.returnDataSuccess(loginUserVo);
    }


    /**
     * 通过Token获取用户信息
     * @param token：token
     * @return：用户信息
     */
    @PostMapping("/getUserByToken")
    public LoginUserVo getUserByToken(@RequestParam("token") String token){
        return userService.getUserByToken(token);
    }


    /**
     * 员工管理页面分页并按照名称模糊查询
     */
    @PostMapping("/list")
    public Result<Map<String,Object>> queryUserByUserName(@RequestBody UserDepRoleDto userDepRoleDto){
        PageInfo<UserDepRoleVo> pageInfo = userService.queryUserByUserName(userDepRoleDto);
        Map<String,Object> map = new HashMap<>();
        map.put("pageInfo",pageInfo);
        return ResultUtils.returnDataSuccess(map);
    }


    /**
     * 用户授权
     * @param userRoleDto
     * @return
     */
    @PostMapping("/createUserRole")
    public Result createUserRole(@RequestBody UserRoleDto userRoleDto){
        Integer userRole = userService.createUserRole(userRoleDto);
        if(userRole<=0){
            return ResultUtils.returnFail("用户授权失败");
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 用户详情展示
     */
    @GetMapping("/getUserByUserId")
    public Result<UserDetailVo> getUserByUserId(@RequestParam("userId") Integer userId){
        UserDetailVo userDetailVo = userService.getUserByUserId(userId);
        return ResultUtils.returnDataSuccess(userDetailVo);
    }


    /**
     * 启用/禁用
     */
    @PostMapping("/updateUserState")
    public Result updateUserState(@RequestParam("id") Integer id){
        Integer integer = userService.updateUserState(id);
        if(integer<=0){
            return ResultUtils.returnResult(ResultEnum.FAIL_USER_STATE);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 修改员工信息
     * @param user
     * @return
     */
    @PostMapping("/updateUserByUserId")
    public Result updateUserByUserId(@RequestBody User user){
        Integer integer = userService.updateUserByUserId(user);
        if(integer <=0){
            return ResultUtils.returnResult(ResultEnum.COMMON_EXCEPTION);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 删除员工
     */
    @PostMapping("/removeUserByUserId")
    public Result removeUserByUserId(@RequestParam("userId") Integer userId){
        return userService.deleteUserByUserId(userId);
    }


    /**
     * 根据用户id查询个人的详细信息
     */
    @GetMapping("/queryUserInfo")
    public Result<UserShowInfoVo> queryUserInfo(@RequestParam("userId") Integer userId){
        UserShowInfoVo userShowInfoVo = userService.queryUserInfo(userId);
        if(userShowInfoVo == null){
            return ResultUtils.returnFail("没有查询到用户信息");
        }
        return ResultUtils.returnDataSuccess(userShowInfoVo);
    }


    /**
     * 验证登录名是否可用
     */
    @PostMapping("/checkLoginUser")
    public Result checkLoginUser(@RequestParam("loginUser") String loginUser){
        Integer count = userService.checkLoginUser(loginUser);
        if(count > 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_CHECK_LOGIN_USER);
        }
        return ResultUtils.returnSuccess();
    }

    /**
     * 修改密码
     */
    @PostMapping("/setPassword")
    public Result setPassword(@RequestBody UpdateUserDto updateUserDto){
        Integer count = userService.setPassword(updateUserDto);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_PASSWORD);
        }
        return ResultUtils.returnSuccess();
    }


    /**
     * 修改用户登录名
     */
    @PostMapping("/setLoginUser")
    public Result setLoginUser(@RequestBody User user){
        if(user == null){
            return null;
        }
        Integer count = userService.setLoginUser(user);
        if(count <= 0){
            return ResultUtils.returnResult(ResultEnum.ERROR_CHECK_LOGIN_USER);
        }
        return ResultUtils.returnSuccess();
    }
}

